let app=getApp()
const db=wx.cloud.database()
const user=db.collection('user')
const formatTime  = require("../../utils/formatTime")
Page({
    data:{
        avatarUrl:'images/head.jpg',
        nickName:'请点击头像登陆',
        openid:'',
        option:['笔记',' 赞过','关注','我的评论'],
        follow:0,
        nleftitems:[],
        nrightitems:[],
        zrightitems:[],
        zrightitems:[],
        myfollow:[],
        skip:0,
        tab:0,
        list:[],
        zleftitems:[]
    },
    aboutUs(){
      wx.navigateTo({
        url: '/pages/center/AboutUsPage/AboutUsPage',
      })
    },
    async load(){
      
      let skip=this.data.skip
            let res=await wx.cloud.callFunction({
              name:'getMyComments',
              data:{
                skip
              }
            })
            skip+=20
            let list=res.result.res
            for(let i=0;i<list.length;i++){
              list[i].time=formatTime.formatTime(list[i].time)
            }
          let openid = app.globalData.openid
          this.get(openid)
            console.log('res',res)
            this.setData({
              list,skip
            })
    },
      onLoad(){
        wx.showLoading({
          title: '加载中',
        })
        console.log('loading')
        console.log(app.globalData.nickName)
        if(app.globalData.nickName){//从全局变量读取用户信息
          console.log(app.globalData.nickName)
          this.setData({
            avatarUrl:app.globalData.avatarUrl,
            nickName:app.globalData.nickName,
            openid:app.globalData.openid
          })
          this.load()
        }else{
          console.log('无信息')
        }
        wx.hideLoading()

      },
      changeItem(e){
        console.log(e)
        this.setData({
            tab:e.currentTarget.dataset.item
        })
    },
    changeTab(e){
        console.log(e)
        this.setData({
            tab:e.detail.current
        })
    },
    async get(openid){
      wx.showLoading({
        title: '努力加载中...',
        mask:true
      })
        let res = await wx.cloud.callFunction({
          name:'getUser',
          data:{openid}
        })
        console.log(res)
        var screenWidth = await wx.getSystemInfoSync().screenWidth; //计算宽度
        this.setData({
      screenWidth: screenWidth / 2-7,
      nleftitems:res.result.nleftitems,
          nrightitems:res.result.nrightitems,
          zleftitems:res.result.zleftitems,
          zrightitems:res.result.zrightitems,
          myfollow:res.result.myfollow,
          follow:res.result.follow,
        })

        wx.hideLoading()
    },
      async login(){//登陆
        console.log(app.globalData.nickName)
        let res=await wx.getUserProfile({
          desc: 'desc',
        })
        console.log('res:',res)
        let {avatarUrl,nickName}=res.userInfo      
        res=await wx.cloud.callFunction({
          name:'login',
          data:{
            avatarUrl,nickName
          }
        })
        console.log('登陆结果',res)
        if(res.result.nickName){
          wx.showToast({
            title: '登陆成功',
          })
          let openid= await user.where({
            nickName:nickName,
            avatarUrl:avatarUrl
          })
          .get()
          openid=openid.data[0].openid
          console.log('openid',openid)
          //写入用户数据到客户端↓↓↓
          this.setData({
            avatarUrl:res.result.avatarUrl,
            nickName:res.result.nickName,
            openid
          })
          wx.setStorage({
            key:'user',
            data:{'avatarUrl':avatarUrl,'nickName':nickName,'openid':openid}
          })
          app.globalData.avatarUrl=avatarUrl
          app.globalData.nickName=nickName
          app.globalData.openid=openid
          console.log(app.globalData.nickName)
          console.log(app.globalData.openid)
          this.get(openid)
        }else{
          wx.showToast({
            title: '登陆失败',
            icon:'error'
          })
        }
      },
      
      jumpPage(e){
            console.log('jumpPage',e)
            wx.navigateTo({
              url: '/pages/orther/content?id='+e.currentTarget.dataset.id,
            })
      },
      async del(e){
        console.log(e)
        let res=await wx.showModal({
          title:'你确定要删除吗？',
          confirmColor:'#f00'
        })
        if(res.confirm){
          console.log('确定')
          let {index,_id}=e.currentTarget.dataset
          let list=this.data.list
          res=await wx.cloud.callFunction({
            name:'delShopComment',
            data:{
              _id
            }
          })
          console.log(res)
          let result,icon='success'
          if(res.result){
            result='删除成功'
            list.splice(index,1)
          }else{
            result='删除失败'
            icon='error'
          }
          wx.showToast({
            title: result,
            icon:icon
          })
          this.setData({
            list
          })
        }
      },
      // 下拉刷新
      onPullDownRefresh(){
        let that=this
        console.log('下拉')
        this.setData({
          skip:0
        })
          wx.stopPullDownRefresh({
            success(){
              that.load()
            }
          })
      }
})
